package com.jdk21.learning;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: jdk21-learning
 * @description:
 * @author: YuKai Fan
 * @create: 2024/8/10 20:31
 **/
public class VirtualThreadsDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        int i = Runtime.getRuntime().availableProcessors();
        System.out.println(i);
    }

    private static void createVirtualThreadPool() throws ExecutionException, InterruptedException {
        try(var executorService = Executors.newVirtualThreadPerTaskExecutor()) {
            var first = executorService.submit(
                    () -> {
                        System.out.println(Thread.currentThread().getName() + ": 开始执行任务");
                        try {
                            TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println(Thread.currentThread().getName() + ": 结束执行任务");
                    }
            );
            var second = executorService.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + ": 测试虚拟线程");
            });
            first.get();
            second.get();
        }
    }

    private static void createVirtualThreadPoolWithFactory() throws ExecutionException, InterruptedException {
        ThreadFactory factory = Thread.ofVirtual().name("demo1-", 0).factory();
        try(var executorService = Executors.newThreadPerTaskExecutor(factory)) {
            var first = executorService.submit(
                    () -> {
                        System.out.println(Thread.currentThread().getName() + ": 开始执行任务");
                        try {
                            TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println(Thread.currentThread().getName() + ": 结束执行任务");
                    }
            );
            var second = executorService.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + ": 测试虚拟线程");
            });
            first.get();
            second.get();
        }
    }

    /**
     * 创建虚拟线程
     */
    private static Thread createVirtualThread1(String name, Runnable runnable) {
        return Thread.ofVirtual()
                // 线程名称
                .name(name)
                .start(runnable);
    }
}
